﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEDangerCategoryService
{
    private readonly SimpleDbContext _context;
    public QHSEDangerCategoryService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEDangerCategoryModel>> GetAsync()
    {
        var violationCategories = await _context.Set<QHSESysDangerCategory>().ToListAsync();
        return MapperHelper.Map<List<QHSEDangerCategoryModel>>(violationCategories);
    }
    public async Task<string> GetIdByItemAsync(string item)
    {
        var id = await _context.Set<QHSESysDangerCategory>().AsNoTracking().Where(u=>u.Item.Equals(item)).Select(u=>u.Id).FirstOrDefaultAsync();
        return id;
    }
    public async Task<QHSEDangerCategoryModel> GetByCodeAsync(string code)
    {
        var query = _context.Set<QHSESysDangerCategory>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(code))
        {
            query = query.Where(u => EF.Functions.Like(u.Code, $"%{code}%"));

        }
        var dangerCategory = await query.FirstOrDefaultAsync();
        var result = MapperHelper.Map<QHSEDangerCategoryModel>(dangerCategory);

        return result;
    }
    public async Task<PageResultModel<QHSEDangerCategoryModel>> GetPageAsync(QHSEDangerCategoryPageInputModel input)
    {
        var result = new PageResultModel<QHSEDangerCategoryModel>();
        var query = _context.Set<QHSESysDangerCategory>().AsQueryable();

        if (!string.IsNullOrEmpty(input.Code))
        {
            query = query.Where(u => EF.Functions.Like(u.Code, $"%{input.Code}%"));

        }


        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        query = query.OrderBy(u => u.Code).Page(input.PageNo, input.PageSize);
        var violationCategories = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEDangerCategoryModel>>(violationCategories);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

    public async Task<List<AntTreeNode>> GetCategoryTreeAsync()
    {
        var query = _context.Set<QHSESysDangerCategory>().AsNoTracking().AsQueryable();

        var gws = await query
            .OrderBy(m => m.Id)
            .ToListAsync();
        List<TreeNode> nodes = MapperHelper.Map<List<TreeNode>>(gws);

        var builder = AntTreeNode.CreateBuilder(nodes, "0");
        return builder.Build();
    }
    public async Task<List<string>> GetChildren(string name)
    {
        var dangerCategory = await _context.Set<QHSESysDangerCategory>()
            .AsNoTracking()
            .FirstOrDefaultAsync(u => u.Item == name);

        if (dangerCategory == null)
        {
            return new List<string>();
        }

        var children = await _context.Set<QHSESysDangerCategory>()
            .Where(u => EF.Functions.Like(u.Code, $"{dangerCategory.Code}%"))
            .Select(u => u.Item)
            .ToListAsync();

        return children;
    }
    public async Task<List<string>> GetChildren(string name,SimpleDbContext context)
    {
        var dangerCategory = await context.Set<QHSESysDangerCategory>()
            .AsNoTracking()
            .FirstOrDefaultAsync(u => u.Item == name);

        if (dangerCategory == null)
        {
            return new List<string>();
        }

        var children = await context.Set<QHSESysDangerCategory>()
            .Where(u => EF.Functions.Like(u.Code, $"{dangerCategory.Code}%"))
            .Select(u => u.Item)
            .ToListAsync();

        return children;
    }

    public async Task<List<string>> GetSon(string name)
    {
        var dangerCategory = await _context.Set<QHSESysDangerCategory>()
            .AsNoTracking()
            .FirstOrDefaultAsync(u => u.Item == name);

        if (dangerCategory == null)
        {
            return new List<string>();
        }
        var sons = await _context.Set<QHSESysDangerCategory>().AsNoTracking().Where(u => u.ParentId==dangerCategory.Id).Select(u => u.Item).ToListAsync();
        return sons;
    }
    public async Task<List<string>> GetSon(string name,SimpleDbContext context)
    {
        var dangerCategory = await context.Set<QHSESysDangerCategory>()
            .AsNoTracking()
            .FirstOrDefaultAsync(u => u.Item == name);

        if (dangerCategory == null)
        {
            return new List<string>();
        }
        var sons = await context.Set<QHSESysDangerCategory>().AsNoTracking().Where(u => u.ParentId == dangerCategory.Id).Select(u => u.Item).ToListAsync();
        return sons;
    }

}
